define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'oil/fuel/index' + location.search,
                    add_url: 'oil/fuel/add'+ location.search,
                    edit_url: 'oil/fuel/edit',
                    del_url: 'oil/fuel/del',
                    multi_url: 'oil/fuel/multi',
                    import_url: 'oil/fuel/import',
                    table: 'oil_fuel',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'store_id', title: __('Store_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'type_text', title: __('Type')},
                        {field: 'guns', title: __('Guns'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Controller.api.formatter.guns},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'chengben_price', title: __('Chengben_price'), operate:'BETWEEN'},
                        {field: 'tjchengben_price', title: __('TJChengben_price'), operate:'BETWEEN'},
                        {field: 'guaji_price', title: __('Guaji_price'), operate:'BETWEEN'},
                        {field: 'original_price', title: __('Original_price'), operate:'BETWEEN'},
                        // {field: 'discount_max', title: __('Discount_max'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                guns: function (value,row) {
                    if(row.guns){
                        var jsonObject = row.guns_array;
                        var html = [];
                        var color, display, label;
                        color='primary';
                        for (i in jsonObject) {
                            display=jsonObject[i].title;
                            label = '<span class="label label-' + color + '">' + display + '</span>';
                            html.push(label);
                        }
                        return html.join(' ');
                    }else{
                        return '暂无';
                    }
                },
            }
        }
    };
    return Controller;
});
